package com.settlement.system.model.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.settlement.system.common.base.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

/**
* <p>
* 有赞账单表
* </p>
*
* @author huangrensen
* @since 2023-10-23
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class YzBill extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 店铺ID
     */
    private Long storeId;

    /**
    * 类型
    */
    private String billType;

    /**
    * 名称
    */
    private String productName;

    /**
    * 业务单号
    */
    private String businessNo;

    /**
    * 支付流水号
    */
    private String payNo;

    /**
    * 关联单号
    */
    private String linkNumber;

    /**
    * 交易来源地
    */
    private String sourcePlace;

    /**
    * 财务主体
    */
    private String billCompany;

    /**
    * 账户
    */
    private String account;

    /**
    * 收入
    */
    private BigDecimal incomeAmount;

    /**
    * 支出
    */
    private BigDecimal payAmount;

    /**
    * 余额
    */
    private BigDecimal restAmount;

    /**
    * 支付方式
    */
    private String payType;

    /**
    * 交易对手
    */
    private String tradeObj;

    /**
    * 渠道
    */
    private String channel;

    /**
    * 下单时间
    */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime orderTime;

    /**
    * 入账时间
    */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime incomeTime;

    /**
    * 操作人
    */
    private String op;

    /**
    * 附加信息
    */
    private String attach;

    /**
    * 备注
    */
    private String remark;

    @TableField(exist = false)
    private String tableName;

    /**
    * 来源
    */
    private String sourceText;

    public static boolean isDate(String field) {
        return "orderTime,incomeTime".contains(field);
    }

    public static boolean isDecimal(String field) {
        return ",incomeAmount,payAmount,restAmount,".contains(","+field+",");
    }

    private static boolean compareDate(LocalDateTime date1, LocalDateTime date2) {
        if (date1 == null && date2 == null) {
            return true;
        }
        if (date1 != null && date2 == null) {
            return false;
        }
        return date1 != null && date1.equals(date2);
    }

    private static boolean compareBigDecimal(BigDecimal value1, BigDecimal value2) {
        if (value1 == null && value2 == null) {
            return true;
        }
        if (Objects.isNull(value2)) {
            value2 = BigDecimal.ZERO;
        }
        return value1 != null && value1.compareTo(value2) == 0;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        YzBill yzBill = (YzBill) o;
        return Objects.equals(billType, yzBill.billType) && Objects.equals(productName, yzBill.productName) && Objects.equals(businessNo, yzBill.businessNo) && Objects.equals(payNo, yzBill.payNo) && Objects.equals(linkNumber, yzBill.linkNumber) && Objects.equals(sourcePlace, yzBill.sourcePlace) && Objects.equals(billCompany, yzBill.billCompany) && Objects.equals(account, yzBill.account) && compareBigDecimal(incomeAmount, yzBill.incomeAmount) && compareBigDecimal(payAmount, yzBill.payAmount) && compareBigDecimal(restAmount, yzBill.restAmount) && Objects.equals(payType, yzBill.payType) && Objects.equals(tradeObj, yzBill.tradeObj) && Objects.equals(channel, yzBill.channel) && compareDate(orderTime, yzBill.orderTime) && compareDate(incomeTime, yzBill.incomeTime) && Objects.equals(op, yzBill.op) && Objects.equals(attach, yzBill.attach) && Objects.equals(remark, yzBill.remark) && Objects.equals(sourceText, yzBill.sourceText);
    }

    @Override
    public int hashCode() {
        return Objects.hash(billType, productName, businessNo, payNo, linkNumber, sourcePlace, billCompany, account, incomeAmount, payAmount, restAmount, payType, tradeObj, channel, orderTime, incomeTime, op, attach, remark, sourceText);
    }
}
